<?php
include_once 'step_3.php';
include_once 'config.php';
include_once 'common_classes.php';

include_once 'layout.php';

function translateAnswer($answer, $isImportant) {
	$result = "";
	switch($answer) {
		case -2:
			$result = '<img src="images/votedisagree.png" title="Strongly disagree" /><img src="images/votedisagree.png" title="Strongly disagree" />';
			break;
		case -1:
			$result = '<img src="images/votedisagree.png" title="Disagree" />';
			break;
		case 0:
			$result = '<img src="images/votenoopinion.png" title="No opinion" />';
			break;
		case 1:
			$result = '<img src="images/voteagree.png" title="Agree" />';
			break;
		case 2:
			$result = '<img src="images/voteagree.png" title="Strongly agree" /><img src="images/voteagree.png" title="Strongly agree" />';
			break;
	}
	
	if($isImportant)
		$result = $result . '<img src="images/voteimportanticon.png" title="This question is important to this candidate" width="14" height="14" />';
	
	return $result;
}

function getBackgroundColor($score) {
	$result = "";
	switch($score) {
		case -4:
			$result = "mf";
			break;
		case -2:
			$result = "mt";
			break;
		case -1:
			$result = "mo";
			break;
		case 0:
			$result = "no";
			break;
		case 1:
			$result = "po";
			break;
		case 2:
			$result = "pt";
			break;
		case 4:
			$result = "pf";
			break;
	}
	
	return $result;
}

function getExplanationClass($bHasExplanation) {
	if($bHasExplanation)
		return " explanation";
		
	return "";
}

function unifyName($name) {
	return str_replace(' ', "_", $name);
}

// translate a total to a success percentage
function getPercentage($total, $numQuestions) {
	$max = ($numQuestions * 2) + (Config::number_of_important_questions * 2);
	$total = ($total + $max) / 2;
	$percentage =  round($total / $max * 100);
	
	return $percentage;
}

$questions = getQuestions();
$ycount = count($questions);// / count($questions);

$rankings = getRankings();
$xcount = count($rankings);// / count($rankings);

$grid = getGrid();

// list the candidates in order, best match first
/*for($i = 0; $i < $xcount; $i++) {
	$candidate = $rankings[$i][0]; 
	echo $candidate->getName() . ' - ' . getPercentage($rankings[$i][1]) . '%<br />';
}*/

openHeader();
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="showhide.js"></script>
<?php 
closeHeader();
openBody(3);
?>
<h1>Results</h1>
Feeling overwhelmed? <a href="step_4_view.php">Go to your voting recommendation &gt;&gt;</a><br /><br />
<b>Their opinion: <img src="images/votedisagree.png" alt="Strongly disagree" /><img src="images/votedisagree.png" alt="Strongly disagree" /> (strongly disagree), <img src="images/votedisagree.png" alt="Disagree" /> (disagree), <img src="images/votenoopinion.png" alt="No opinion" /> (no opinion), <img src="images/voteagree.png" alt="Agree" /> (agree), <img src="images/voteagree.png" alt="Strongly agree" /><img src="images/voteagree.png" alt="Strongly agree" /> (strongly agree), <img src="images/voteimportanticon.png" alt="Important" width="14" height="14" /> (important question)<br /><img src="images/balloon.png" alt="Explanation" /> (Mouse over for the explanation of the answer)<br />
Match with you:
<?php echo "<span class=" . getBackgroundColor(2) . ">"; ?>Perfect match<?php echo "</span>"; ?>, 
<?php echo "<span class=" . getBackgroundColor(1) . ">"; ?>good match<?php echo "</span>"; ?>, 
<?php echo "<span class=" . getBackgroundColor(0) . ">"; ?>no opinion<?php echo "</span>"; ?>, 
<?php echo "<span class=" . getBackgroundColor(-1) . ">"; ?>bad match<?php echo "</span>"; ?>, 
<?php echo "<span class=" . getBackgroundColor(-2) . ">"; ?>terrible match<?php echo "</span>"; ?> 
</b><br /><br />

<table border="1">
<?php
$firstRow = true;
$firstColumn = true;
for($y = 0; $y < $ycount + 1; $y++) {
	echo '<tr>';
	for($x = 0; $x < $xcount + 1; $x++) {
		// correct count for the +1 in the loops
		$myx = $x - 1;
		$myy = $y - 1;
		
		if($firstRow) {
			if(!$firstColumn) {
			$candidate = $rankings[$myx][0];
			
			echo '<td class="header '. unifyName($candidate->getName()) .'">';
			} else {
				echo '<td class="header">';
			}
		}
		elseif($firstColumn)
			echo '<td class="question">';
			
		if($firstRow && $firstColumn) {
			echo '<span class="hide show" style="display:none;"><small><a href="#" onclick="javascript:showAll()">Some questions or candidates are hidden. Click here to show all.</a></span>';
		}
		
		// the top row - the candidate names go here
		if($firstRow && !$firstColumn) {
			$candidate = $rankings[$myx][0];
			$percentage = getPercentage($rankings[$myx][1], $ycount);
			echo '<a href="https://gate.eveonline.com/Profile/' . $candidate->getName() . '"><img src="https://image.eveonline.com/Character/' . $candidate->getUserID() . '_64.jpg" border="0" /></a><br />';
			echo '<a href="' . $candidate->getURL() . '">' . $candidate->getName() . ' (' . $percentage . '%)</a>';
			echo '<small><br /> <br /><a href="#" onclick="javascript:hideCandidate(\'' . unifyName($candidate->getName()) . '\');">hide candidate</a></small>';
		}
		
		// the first column - the questions go here
		if(!$firstRow && $firstColumn) {
			echo '<span class="qstring ' . $questions[$myy][1] . '">' . $questions[$myy][0] . '</span>';
			echo '<span class="hide"><small><a href="#" onclick="javascript:hideQuestion(\'' . $questions[$myy][1] . '\');">hide question</a></small></span>';
		}
		
		// its a regular cell, set its background color and display the answer 
		if(!$firstRow && !$firstColumn) {
			$candidate = $rankings[$myx][0];
			$questionid = $questions[$myy][1];
			$score = $grid->getScore($questionid, $candidate->getName());
			$cname = unifyName($candidate->getName());
			
			$bHasExplanation = false;
			if($score[3] != "")
				$bHasExplanation = true;
				
			echo '<td class="' . getBackgroundColor($score[1] / ($score[2] + 1)) . getExplanationClass($bHasExplanation) . " $cname $questionid" . '">'; // divide by 2 (is important) or 1 (is not) to negate the higher score for important questions
			
			if($bHasExplanation)
				echo '<span onmouseover="tooltip.show(\'<b>' . addslashes($candidate->getName()) . ' says:</b><br />' . addslashes($score[3]) . '\');" onmouseout="tooltip.hide();">';
				
			echo translateAnswer($score[0], $score[2]);
			
			if($bHasExplanation)
				echo '</span>';
		}
		
		// ending x loop, end cell
		$firstColumn = false;
		echo '</td>';
	}
	// ending y loop, end row
	$firstRow = false;
	$firstColumn = true;
	echo '</tr>';
} 
?>
</table>
<script type="text/javascript" language="javascript" src="tooltip.js"></script>

<?php 
closeHolder();
closeBody(); 
?>